package com.dycx.food.converter;

import cn.hutool.core.bean.BeanUtil;
import com.dycx.common.vendor.oss.AliyunOss;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dycx.food.entity.GoodsClassDO;
import com.dycx.food.vo.GoodsClassVO;
import com.dycx.food.dto.GoodsClassDTO;

/**
 * 商品分类(GoodsClass)实体转换类
 *
 * @author wuzongbing
 * @since 2025-02-13 03:26:32
 */


@Data
public class GoodsClassConverter implements Serializable {
    public static GoodsClassDO dtoToEntity(GoodsClassDTO dto) {
        if (dto == null) return null;
        GoodsClassDO entity = BeanUtil.toBean(dto, GoodsClassDO.class);
        entity.setImage(AliyunOss.delDomain(entity.getImage()));
        return entity;
    }

    public static GoodsClassVO entityToVo(GoodsClassDO entity) {
        if (entity == null) return null;
        GoodsClassVO vo = BeanUtil.toBean(entity, GoodsClassVO.class);
        vo.setImage(AliyunOss.addDomain(vo.getImage()));
        return vo;
    }

    public static List<GoodsClassVO> entityToVoList(List<GoodsClassDO> entityList) {
        List<GoodsClassVO> voList = new ArrayList<>();
        for (GoodsClassDO entity : entityList) {
            voList.add(entityToVo(entity));
        }
        return voList;
    }

    public static Page<GoodsClassVO> entityToVoPage(Page<GoodsClassDO> page) {

        List<GoodsClassVO> newRecords = new ArrayList<>();

        List<GoodsClassDO> records = page.getRecords();
        for (GoodsClassDO entity : records) {
            newRecords.add(entityToVo(entity));
        }

        Page<GoodsClassVO> voPage = new Page<>();
        voPage.setRecords(newRecords);
        voPage.setPages(page.getPages());
        voPage.setTotal(page.getTotal());
        voPage.setSize(page.getSize());
        voPage.setCurrent(page.getCurrent());

        return voPage;
    }

}
